package com.wdf.dota;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wdf.bean.Comment;
import com.wdf.bean.ForumPosting;
import com.wdf.rest.service.DotaDiscussionRestClient;

public class SubmitForumPostingActivity  extends Activity{
	
	Button returnCommentBtn;
	Button submitCommentBtn;
	
	TextView myUsernameView ;
	
	EditText myTitle;
	EditText myComment;
	
	ForumPostingRestService service  ;
	String topic;
	String username;
	
      String accountId;

	
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.submit_forumposting);
		submitCommentBtn =  (Button) findViewById(R.id.submit_comment);
		submitCommentBtn.setOnClickListener(submitCommentButtonListener);
		
		returnCommentBtn =  (Button) findViewById(R.id.return_comment);
		returnCommentBtn.setOnClickListener(returnCommentButtonListener);
		
		myUsernameView = (TextView)this.findViewById(R.id.my_username);
		myTitle =  (EditText)this.findViewById(R.id.my_title);
		myComment =  (EditText)this.findViewById(R.id.my_comment);
		
		SharedPreferences sharedPreferences = getSharedPreferences("wonderful",
				Context.MODE_PRIVATE);
		username = sharedPreferences.getString("username", "");
		accountId = sharedPreferences.getString("accountid", "");
		myUsernameView.setText(username+" :");
	
		Bundle bundle = this.getIntent().getExtras();
		topic=	bundle.getString("topic");
		service  = new ForumPostingRestService(topic,this);
		

	}
	
	OnClickListener submitCommentButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			ForumPosting posting = new ForumPosting();
              String title  = myTitle.getText().toString();
			 String content = myComment.getText().toString();
			 if(title == null || "".equals(title.trim())){
				 Toast.makeText(SubmitForumPostingActivity.this, "请输入标题!", 2).show();
				 return ;
			 }
			 
			 if(content == null || "".equals(content.trim())){
				 Toast.makeText(SubmitForumPostingActivity.this, "请输入评论内容!", 2).show();
				 return ;
			 }
			 if(title.length()>=100){
				 Toast.makeText(SubmitForumPostingActivity.this, "标题太长,超过最大限制!!", 2).show();
				 return ;
			 }
			 if(content.length()>=1000){
				 Toast.makeText(SubmitForumPostingActivity.this, "评论内容太长,超过最大限制!!", 2).show();
				 return ;
			 }
			 
			   boolean checkNickname = false;

			   posting.setTitle(title);
			   posting.setContent(content);
			   posting.setTopic(topic);
			   waitingDialog() ;
			 service.addPosting(posting);
		}
	};
	
	private ProgressDialog dialog;

	private void waitingDialog() {
		dialog = new ProgressDialog(this);
		dialog.setIcon(R.drawable.smile);
		dialog.setTitle("请稍等");
		dialog.setMessage("正在发贴.....");
		dialog.setCancelable(true);
		dialog.show();

	}
	
	
	OnClickListener returnCommentButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			SubmitForumPostingActivity.this.finish();
		}
	};
	
	class ForumPostingRestService {
        private String topic ;
        private Context  context;
        
        public ForumPostingRestService(String topic, Context context){
        	this.topic = topic;
        	this.context = context;
        }

      
		 public  void  addPosting(final ForumPosting forumPosting){

				 JSONObject object = new JSONObject();
				 try {
					  
					 object.put("content", forumPosting.getContent());
					 object.put("title", forumPosting.getTitle());
					 object.put("topic", forumPosting.getTopic());
					 StringEntity entity = new StringEntity(object.toString(), "UTF-8");
					 DotaDiscussionRestClient.post(SubmitForumPostingActivity.this, "/dota/forumpostings.json?accountId="+accountId, entity, "application/json;charset=UTF-8", new JsonHttpResponseHandler() {
				            @Override
				            public void onSuccess(JSONObject object) {
				            	 if(object!=null ){
				            		   try {
										int id = object.getInt("id");
										if(id>0){
											Toast.makeText(context, "发贴成功!", 2).show();
							            		SubmitForumPostingActivity.this.setResult(4, SubmitForumPostingActivity.this.getIntent());
							            		SubmitForumPostingActivity.this.finish();
										}else{
											 Toast.makeText(context, "发贴失败!", 2).show();
										}
									} catch (JSONException e) {
										 Toast.makeText(context, "发贴失败!", 2).show();
									}finally{
										dialog.dismiss();
									}
				            		   
				            	 }
				            	
				            }
				         public void onFailure(Throwable error){
				        		dialog.dismiss();
				        		 Toast.makeText(context, "发贴失败!", 2).show();
				         }  
				            
				        });
 
				} catch (Exception e) {
					
					 Toast.makeText(context, "发贴失败!", 2).show();
				}
				 
			 }	
	}
}
